package com.myapplication.ui.showfragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.myapplication.R;
import com.myapplication.bean.ShaiwuBean;
import com.myapplication.config.HUrlConfig;
import com.myapplication.ui.infofragment.infowebactivity.InfoWebActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zitaojiang on 2016/10/6.
 */
public class Fragment_Show extends Fragment implements Contract_Show.View {
    private Contract_Show.Presenter mPresenter;
    private Map<String,String> params=new HashMap<>();
    private Map<String,String> params2=new HashMap<>();
    private ShaiWuFragmentAdapter adapter;
    private int page=0;
    private long lastpubtime;
    private PullToRefreshListView lv_shaiwu;
    private ListView mListView;
    private List<ShaiwuBean.DataBean.ListBean> shaiwuBeanList=new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new Presenter_Show(this);
        params.put(HUrlConfig.key.PAGE, HUrlConfig.DefaultValue.PAGE);
        params.put(HUrlConfig.key.LASTPUBTIME, HUrlConfig.DefaultValue.LASTPUBTME);
        params.put(HUrlConfig.key.APP_VERSION, HUrlConfig.DefaultValue.APP_VERSION);
        params.put(HUrlConfig.key.PLATFORM, HUrlConfig.DefaultValue.PLATFORM);
        mPresenter.GetData(params);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_show, container, false);
        lv_shaiwu= (PullToRefreshListView) view.findViewById(R.id.lv_shaiwu);
        mListView=lv_shaiwu.getRefreshableView();
        lv_shaiwu.setMode(PullToRefreshBase.Mode.BOTH);
        initEvent();
        return view;
    }

    private void initEvent() {
        lv_shaiwu.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mPresenter.GetData(params);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                lastpubtime=shaiwuBeanList.get(shaiwuBeanList.size()-1).getShaidan().getPubTime();
                params2.put(HUrlConfig.key.LASTSHOWID, String.valueOf(lastpubtime));
                params2.put(HUrlConfig.key.PAGE,String.valueOf(page));
                mPresenter.GetMoreData(params2);

            }
        });

        lv_shaiwu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(getActivity(), InfoWebActivity.class);
                intent.putExtra("url",shaiwuBeanList.get(i-1).getShaidan().getUrl());
                intent.putExtra("title",shaiwuBeanList.get(i-1).getShaidan().getTitle());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onGetDataSuccess(final List<ShaiwuBean.DataBean.ListBean> shaiwuBeanList) {

        if(page==0){
            this.shaiwuBeanList.clear();
            this.shaiwuBeanList.addAll(shaiwuBeanList);
            adapter=new ShaiWuFragmentAdapter(getActivity(),shaiwuBeanList);
            adapter.setShaiwuList(this.shaiwuBeanList);
            mListView.setAdapter(adapter);
        }else{
            this.shaiwuBeanList.addAll(shaiwuBeanList);
            adapter.setShaiwuList(this.shaiwuBeanList);
            adapter.notifyDataSetChanged();
        }
        lv_shaiwu.onRefreshComplete();
    }


    @Override
    public void onGetDataFail(String error) {
        Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
    }


}
